package zero.tech.games.mj.util;



import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import zero.tech.games.logic.common.Room;
import zero.tech.games.logic.common.RoomCache;
import zero.tech.games.mj.data.MjGameBase;
import zero.tech.games.mj.data.consts.GameConst;
import zero.tech.games.mj.data.consts.MjConst;
import zero.tech.games.mj.data.entity.PlayerData;

/**
 * 游戏工具类
 * @author Administrator
 *
 */
public class GameUtils {
	
	/**
	 * 对象转化成int
	 * @param obj
	 * @return
	 */
	public static Integer getInteger(Object obj) {
		int value = 0;
		if (obj != null && !obj.equals(""))
		{
			value = Integer.parseInt(obj.toString());
		}
		return value;
	}
	
	/**
	 * 字符串转换为LIST
	 * @param value 字符串
	 * @param sp 通配符
	 * @return
	 */
	public static List<String> getStrList(String value, String sp) {
		List<String> list = new ArrayList<String>();
		String[] lists = value.split(sp);
		if (lists != null) {
			for (String id : lists) {
				list.add(id);
			}
		}
		return list;
	}
	
	/**
	 * 字符串转换为LIST
	 * @param value 字符串
	 * @param sp 通配符
	 * @return
	 */
	public static int[] getIntArr(String value, String sp) {
		int[] list = null;
		String[] lists = value.split(sp);
		if (lists != null) {
			list = new int[lists.length]; 
			for (int i = 0; i < lists.length; i++) {
				list[i] = getInteger(lists[i]);
			}
		}
		return list;
	}
	
	/**
	 * 转换成字符串
	 * @param list
	 * @return
	 */
	public static String getStrByArr(List<Integer> list) {
		String str = "";
		if (list != null) {
			for (int i = 0; i < list.size(); i++) {
				str += list.get(i) + (i == list.size() - 1 ? "" : ",") ;
			}
		}
		return str ;
	}
	
	/**
	 * 转换成字符串
	 * @param list
	 * @return
	 */
	public static String getStrByArr(List<String> list, String sp) {
		String str = "";
		if (list != null) {
			for (int i = 0; i < list.size(); i++) {
				str += list.get(i) + (i == list.size() - 1 ? "" : sp) ;
			}
		}
		return str ;
	}
	
	/**
	 * 获取类型字符串 (1,2,3)2碰,3明杠,4胡,5暗杠,6点杠
	 * @param tmp
	 * @return
	 */
	public static String getTypeInfo(List<String> tmp) {
		String info = "";
		if (!tmp.isEmpty()) {
			for (int i = 0; i < tmp.size(); i++) {
				if (i < tmp.size() - 1) {
					info += tmp.get(i) + "_" + "|";
				} else {
					info += tmp.get(i) + "_";
				}
			}
		}
		return info;
	}
	
	/**
	 * 将list<integer> 转换成为 以 | 分割的string
	 * @param lis
	 * @return
	 */
	public static String listStrToStringByl(List<String> lis){
		String str = "";
		for (int i = 0; i < lis.size(); i++) {
			if (i< lis.size()-1) {
				str += lis.get(i)+"|";
			}else {
				str += lis.get(i);
			}
		}
		return str;
	}
	
	/**
	 * 将list<integer> 转换成为 以 , 分割的string
	 * @param lis
	 * @return
	 */
	public static String listToString(List<Integer> lis){
		String str = "";
		for (int i = 0; i < lis.size(); i++) {
			if (i< lis.size()-1) {
				str += lis.get(i)+",";
			}else {
				str += lis.get(i);
			}
		}
		return str;
	}
	
	/**
	 * 添加明杠信息
	 * @param card
	 * @param vo
	 */
	public static String addMingGangInfo(int card, PlayerData player) {
		String reStr = "";
		for (int i = 0; i < player.getCtrlCard().size(); i++) {
			List<String> list = GameUtils.getStrList(player.getCtrlCard().get(i),
					"_");
			if (GameUtils.getInteger(list.get(1)) == MjConst.PENG) {
				String chair = list.get(3);
				List<String> cards = GameUtils.getStrList(list.get(0), ",");
				if (!cards.isEmpty()) {

					int[] p1 = CardUtil.getCard(GameUtils.getInteger(cards
							.get(0)));
					int[] p2 = CardUtil.getCard(card);
					if (p1[3] == p2[3] && p1[1] == p2[1]) {
						player.getCtrlCard().remove(i);
						reStr = list.get(0) + "," + card;
						String str = reStr + "_" + MjConst.GANG + "_"
								+ player.getUid() + "_" + chair;
						player.addCtrlCard(str);
						break;
					}
				}
			}
		}
		return reStr;
	}

	/**
	 * 检查所有玩家是否有特殊操作
	 * @param gameBase
	 * @return
	 */
	public static boolean check(MjGameBase gameBase) {
		for (PlayerData o : gameBase.getPlayerMap().values()) {
			if (!o.getActionArr().isEmpty())
				return false;
		}
		return true;
	}



	/**
	 * 流局剩余牌数量
	 * @param type 奖码数（0"",1"一码全中",2"",3"博码",4"4码",5"",6"6码"）
	 * @return 牌数量
	 */
	public static int flowBureau(int type) {
		int num = 0;
		if (type == 1) {
			num = 1;
		} else if (type == 3) {
			num = 10;
		} else if (type == 4) {
			num = 4;
		} else if (type == 6) {
			num = 6;
		}
		return num;
	}

	/**
	 * 根据玩家id获取下一个玩家id
	 * @param room
	 * @param uid
	 * @return
	 */
	public static int getNextPosByUid(Room room, int uid) {
		Integer find=null;
		int pos=getPosByUid(room, uid);
		int max_player=room.getMax_player();
		Map<Integer, Integer> seats=room.getSeats();
		RoomCache roomCache = room.getRoomCache();
		Set<Integer> watchers=roomCache.getWatchers();
		Set<Integer> taotais=roomCache.getTaotais();
		for(int i=1;i<=max_player;i++) {
			int pos1 = (pos + i >max_player ? pos + i - max_player : pos + i);
			Integer next=seats.get(pos1);
			if(next!=null&&!watchers.contains(next)&&!taotais.contains(next)) {
				find=next;
				break;
			}
		}
		return find;
	}
	
	/**
	 * 根据玩家id获取位置
	 * @param room
	 * @param uid
	 * @return
	 */
	public static Integer getPosByUid(Room room,int uid) {
		Integer pos=null;
		Map<Integer,Integer> map=room.getSeats();
		for(Entry<Integer,Integer> entry:map.entrySet()) {
			if(entry.getValue()!=null&&uid==entry.getValue()) {
				pos=entry.getKey();
				break;
			}
		}
		return pos;
	}
	
	/**
	 * 组装胡牌字符串
	 * @param winType 1平胡,2大对,3清一色,4小七对,5龙七对,6金钩钓
	 * @param huType 1自摸2接炮,0未胡,-1查大叫
	 * @return
	 * @throws Exception
	 */
	public static String getWinStr(PlayerData player, Room room){
		MjGameBase gameBase = (MjGameBase) room.getGameBase();
		String str = "";
		switch (player.getHuType()) {
		case GameConst.WIN_ZIMO:
			str += "自摸";
			List<Integer> guiList = CardUtil.getObtainGhost(player.getHandCard(),room);
			if(guiList.size() == 0){
				str +=" 无鬼牌";
			}
			break;
		case GameConst.WIN_JIEPAO:
			str += "接炮";
			break;
		case GameConst.WIN_WEIHU:
			str += "未胡";
			break;
		}

		/** 添加和牌的类型 */
		if (player.getWinTypeStr() == null || "".equals(player.getWinTypeStr().trim())) {
			// str += "(" +"" + ") ";;
		}else {
			str += "(" + player.getWinTypeStr() + ") ";
		}

		int[] tmp = new int[] { 0, 0, 0 };
		for (int i = 0; i < player.getCtrlCard().size(); i++) {
			List<String> list = GameUtils.getStrList(player.getCtrlCard().get(i),
					"_");
			int type = GameUtils.getInteger(list.get(1));
			switch (type) {
			case MjConst.AN_GANG:
				tmp[0] += 1;
				break;
			case MjConst.GANG:
				tmp[1] += 1;
				break;
			case MjConst.DI_GANG:
				tmp[2] += 1;
				break;
			}
		}

		if (tmp[0] > 0) {
			// str += " 暗杠x" + tmp[0];
		}
		if (tmp[1] > 0) {
			// str += " 明杠x" + tmp[1];
		}
		if (tmp[2] > 0) {
			// str += " 点杠x" + tmp[2];
		}
		// vo.setHandCard(RoomLogic.listToVector(GameUtils.getNewList(vo.getHandCard())));
		// 统计自己点了几家炮 点1.2.3胡
		List<Integer> huL = new ArrayList<Integer>();
		for (PlayerData VO : gameBase.getPlayerMap().values()) {
			if (player.getUid() != VO.getUid()) {
				if (VO.getHuType() == 4) {
					PlayerData loseVO = gameBase.getPlayerUid(VO.getLoseArr().get(0));
					if (loseVO.getUid() == player.getUid())
						huL.add(VO.getHuSort());
				}
			}
		}
		
		Collections.sort(huL);
		String strF = "";
		if (huL.size() == 1) {
			strF = " 点" + huL.get(0) + "胡";

		}

		else if (huL.size() > 1) {
			for (Integer integer : huL) {
				strF += integer + ".";
			}
			if (!"".equals(strF))
				strF = strF.substring(0, strF.length() - 1);
			strF = "  点" + strF + "胡";
		}
		return str + strF + player.getGangStr();
	}
	
	/**
	 * 判断是否4个人都没有胡牌
	 * @param gameBase
	 * @return
	 */
	public static boolean checkHaveWin(MjGameBase gameBase) {
		for (PlayerData vo : gameBase.getPlayerMap().values()) {
			if (vo.isWin())
				return true;
		}
		return false;
	}
}
